<template>
  <div></div>
</template>

<script>
import { oauth } from "@/apis/login";
import { checklogin } from "@/apis/login";
import { wechat } from "@/apis/login";
import apiConfig from "../../config/api.config"
export default {
  data() {
    return {
      apiConfig,
      parmas: {
        code: "",
        ty: 2,
      },
      appid:'',
      domain:null
    };
  },
  methods: {
    getCode() {
      // 非静默授权，第一次有弹框
      this.parmas.code = "";
      var local = window.location.href; // 获取页面url
       var appid = this.appid;
      this.parmas.code = this.getUrlCode("code"); // 截取code

      if (this.parmas.code == null || this.parmas.code === "") {
        // 如果没有code，则去请求
        window.location.replace(
          `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(
            local
          )}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
        );
      } else {
        // 你自己的业务逻辑
        this.parmas.code = this.getUrlCode("code");
      }
    },
    getDomain(name) {
     if(process.env.NODE_ENV==='development'){
        this.domain = 3;
        sessionStorage.setItem('domain',this.domain);
        sessionStorage.setItem('webTitle',"999");
      }
      else {
        this.domain = this.getUrlCode("domain")
        sessionStorage.setItem('domain',this.domain)
        if(this.domain == null){
          this.$toast('非法操作');
        }else{
          wechat({domain:this.domain})
            .then(res => {
              if(res.codes == 200){
                this.appid = res.data.appid
                sessionStorage.setItem('webTitle',res.data.name)
                this.getCode();
              }
            })
            .catch(err =>{
              if(err.msg){
                this.$toast(err.msg);
              }else{
                this.$toast('网络错误');
              }
            })
        }
      }
    },
    getUrlCode(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
  },
  watch: {
    "parmas.code": {
      handler(newName, oldName) {
        if (newName) {
          oauth({ code: newName, pid: 0, ty: 2 ,  domain:this.domain})
            .then((res) => {
              sessionStorage.setItem("token", res.data.token);
              sessionStorage.setItem("bindtoken", res.data.bindtoken);
              if (res.data.ty == 2) {
                if (res.codes == "200" && res.data.token == "") {
                  window.location.href =
                    window.location.origin + "/adviser/#/login";
                  window.location.origin + window.location.pathname;
                  console.log(sessionStorage.getItem("token"))
                  
                } else if (res.codes == "200" && res.data.token != "") {
                 
                   checklogin({token:sessionStorage.getItem("token")})
                  .then((res) => {
                    //  if(res.data.pid > 0){
                    //       window.location.href =
                    //       window.location.origin + "/adviser/#/";
                    //     }
                    //     else{
                    //         sessionStorage.clear()
                    //         window.location.href =
                    //         window.location.origin + "/adviser";
                    //     }
                     if(res.data.ty == 2){
                       if(res.data.pid > 0){
                            window.location.href =
                            window.location.origin + "/adviser/#/?domain=" + this.domain;;
                          }
                          else{
                              sessionStorage.clear()
                              window.location.href =
                              window.location.origin + "/adviser/#/login?domain=" + this.domain;;
                          }
                     }else{
                       if(res.data.ty == 0) {
                         window.location.href = apiConfig.baseUrl + '/patient?domain=' + this.domain
                       }else if(res.data.ty == 1){
                         window.location.href = apiConfig.baseUrl + '/doctor?domain=' + this.domain
                          sessionStorage.setItem("token", res.data.token);
                          sessionStorage.setItem("bindtoken", res.data.bindtoken);
                       }
                     }
                  })
                  .catch((err) => {
                    // alert('123')
                    this.$toast(err.msg); 
                    // sessionStorage.clear()
                  });
                }
              }
              else {
                  if (res.data.ty == 0) {
                    setTimeout(function () {
                      window.location.href =apiConfig.baseUrl + '/patient?domain=' + this.domain
                    }, 1000);
                    sessionStorage.setItem("bindtoken", res.data.bindtoken);
                  } else if (res.data.ty == 1) {
                    setTimeout(function () {
                      window.location.href = apiConfig.baseUrl + '/doctor?domain=' + this.domain
                    }, 1000);
                    sessionStorage.setItem("token", res.data.token);
                    sessionStorage.setItem("bindtoken", res.data.bindtoken);
                  }
                }
            })
            .catch((err) => {
              // window.location.href =
              //   window.location.origin + window.location.pathname + "#/login";
              // alert('失败了')
              this.$toast(err.msg);
            });
        }
      },
      immediate: true,
      deep: true,
    },
  },
  mounted() {
    sessionStorage.setItem("webUrl",localStorage.getItem('webUrl'))
    this.getDomain();
  },
};
</script>

<style lang="less"></style>
